package timev

import (
	"time"
)

const ForMatTime = "2006-01-02 15:04:05"

type Timer interface {
	Start(t time.Time) time.Time
	End(t time.Time) time.Time
}

type Timev struct {
}

// Start 当天开始时间
func (ts Timev) Start(t time.Time) time.Time {
	return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
}

// End 当天结束时间
func (ts Timev) End(t time.Time) time.Time {
	return time.Date(t.Year(), t.Month(), t.Day(), 23, 59, 59, 59, t.Location())
}

// GetNowDay 获取当天
func GetNowDay() time.Time {
	return time.Now()

}

// GetWeekStartDay 获取当周周一
func GetWeekStartDay() time.Time {
	t := GetNowDay()
	offset := int(time.Monday - t.Weekday())
	if offset > 0 {
		offset = -6
	}
	return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.Local).
		AddDate(0, 0, offset)
}

// GetWeekEndDay 获取当周周日
func GetWeekEndDay() time.Time {
	t := GetNowDay()
	// 获取当前周的周日的时间
	// 注意：周日在time.Weekday的值为0
	return t.AddDate(0, 0, -int(t.Weekday())+7)
}

// GetMonthStartDay 获取当月开始时间
func GetMonthStartDay() time.Time {
	now := GetNowDay()
	// 获取本月的第一天
	return time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
}

// GetMonthEndDay 获取当月结束时间
func GetMonthEndDay() time.Time {
	// 获取本月的第一天
	firstOfMonth := GetMonthStartDay()
	// 获取本月的最后一天
	return firstOfMonth.AddDate(0, 1, -1)
}

// GetTimeRFTString 2024-10-08T23:06:46+08:00" -> 2024-10-08 12:00:06
func GetTimeRFTString(str string) time.Time {
	tmp, _ := time.ParseInLocation(time.RFC3339, str, time.Local)
	return tmp
}

// GetTimeString 2024-10-08T23:06:46+08:00" -> 2024-10-08 12:00:06
func GetTimeString(str, format string) time.Time {
	if format == "" {
		format = ForMatTime
	}
	tmp, _ := time.ParseInLocation(format, str, time.Local)
	return tmp
}
